package ds.factcom.domain.invoice;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import ds.factcom.domain.client.Client;
import ds.factcom.domain.item.Item;

/**
 * @author Javier De Luca, Maximiliano Herrera, Matias Samblancat
 */
public class InvoiceVip extends Invoice {

	
	private static final long serialVersionUID = -3354052459170828403L;

	public InvoiceVip(Client client, List<Item> listItem) {
		super(client, listItem);
	}

	@Override
	protected Float generateMountAbsoluty() {
		
		List<Item> items = new ArrayList<Item>();
		
		Item itemtAux = this.getFirstItem();
		
		items.add(itemtAux);
		
		// saco aca los productos mas caros que se encuentran en los items y los guardo en una lista de items
		for (Iterator<Item> iterator = this.getListItem().iterator(); iterator.hasNext();) {
			Item item = (Item) iterator.next();
			int compareTo = itemtAux.getUnitPrice().compareTo(item.getUnitPrice());
			
			if (compareTo == -1){
				itemtAux = item;
				items.clear();
				items.add(itemtAux);
				
			}else if(compareTo == 0){
				items.add(item);
			}
		}
		
		// aca lo que hago es al total del precio del producto del item, de los mas caros
		// le aplico el descuento, si existen mas de un producto con el precio mas caro.
		Float mountProductsMostExpensive = 0F;
		for (Iterator<Item> iterator = items.iterator(); iterator.hasNext();) {
			Item item = (Item) iterator.next();
			mountProductsMostExpensive += item.getTotalPrice();
		}
		mountProductsMostExpensive = mountProductsMostExpensive / 1.25F;
		
		Float difference = this.getMountNet() - mountProductsMostExpensive;
		
		Float mountNet = this.getMountNet() - difference;
		
		if (this.getListItem().size()<10){
			mountNet = mountNet * 1.04F;  
		}else{
			mountNet = mountNet * 1.02F;
		}
		
		return mountNet;
	}

}
